package com.ym.controller.management;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ym.core.domain.APIResponse;
import com.ym.core.domain.BaseController;
import com.ym.medical.domain.entity.HospitalCodeMapEntity;
import com.ym.medical.domain.param.HospitalCodeMapParam;
import com.ym.medical.service.IHospitalCodeMapSV;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 导航数据管理
 */
@Api(tags = "导航-医院编码映射数据管理")
@RestController
@RequestMapping("/management/navHosCode")
public class NavHosCodeMapManagement extends BaseController {

	private static final Logger LOGGER = LoggerFactory.getLogger(NavHosCodeMapManagement.class);
	@Autowired
	private IHospitalCodeMapSV hospitalCodeMapSV;

	@ApiOperation("分页查询")
	@GetMapping("/getPageList")
	@ResponseBody
	public APIResponse<PageInfo<HospitalCodeMapEntity>> getPageList(
			@ApiParam(name = "pageNo", value = "页码", required = true)
			@RequestParam(name = "pageNo", required = true, defaultValue = "1")
					int pageNo,
			@ApiParam(name = "limit", value = "每页数量", required = false)
			@RequestParam(name = "limit", required = false, defaultValue = "10")
					int limit,
			@ApiParam(name = "hospitalCodeMapModel", value = "医院编码映射信息", required = false)
					HospitalCodeMapParam hospitalCodeMapModel) {
		PageHelper.startPage(pageNo,limit);
		List<HospitalCodeMapEntity> hospital = hospitalCodeMapSV
				.selectList(hospitalCodeMapModel.covert());
		return successPage(hospital);
	}

	@ApiOperation("新增")
	@PostMapping("/addHosCodeMap")
	@ResponseBody
	public APIResponse addHosCodeMap(
			@ApiParam(name = "hospital", value = "医院编码映射信息", required = true)
			@Validated(value = HospitalCodeMapParam.Add.class) @RequestBody HospitalCodeMapParam hospital) {
		long rows = hospitalCodeMapSV.insert(hospital.covert());
		if (rows != 1) {
			return failMsg("添加失败");
		}
		return successMsg("添加成功");
	}

	@ApiOperation("删除")
	@PostMapping("/deleteHosCodeMap")
	@ResponseBody
	public APIResponse deleteHosCodeMap(
			@ApiParam(name = "hospIds", value = "医院ids数组", required = true) @RequestBody Long[] hospIds) throws Exception {
		int rows = hospitalCodeMapSV.deleteByIds(hospIds);
		if (rows == 0) {
			return failMsg("删除失败");
		}
		return successMsg("删除成功");
	}

	@ApiOperation("修改")
	@PostMapping("/updateHosCodeMap")
	@ResponseBody
	public APIResponse updateHosCodeMap(
			@ApiParam(name = "hospital", value = "医院编码映射信息", required = true)
			@Validated(value = HospitalCodeMapParam.Update.class) @RequestBody HospitalCodeMapParam hospital) {
		int rows = hospitalCodeMapSV.updateById(hospital.covert());
		if (rows != 1) {
			return failMsg("更改失败");
		}
		return successMsg("更改成功");
	}

	@ApiOperation("查询")
	@GetMapping("/viewHosCodeMap")
	@ResponseBody
	public APIResponse<HospitalCodeMapEntity> viewHosCodeMap(
			@ApiParam(name = "id", value = "医院id", required = true) @RequestParam Long id) {
		return successData(hospitalCodeMapSV.selectById(id));
	}
}
